package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemPostService systemPostService;

    @Override
    public IPage<SystemUserItemVo> pageSystemUserItemVo(long current, long size, SystemUserQueryVo queryVo) {
        Page<SystemUserItemVo> page = new Page<>(current, size);
        return baseMapper.pageSystemUserItemVo(page, queryVo);

    }

    @Override
    public SystemUserItemVo getSystemUserItemVoById(Long id) {
        return baseMapper.getSystemUserItemVoById(id);
    }

    @Override
    public SystemUserItemVo getSystemUserItemVoByIdV2(Long id) {
        SystemUser systemUser = this.getById(id);
        //健壮性校验
        if (systemUser == null) {
            return null;
        }
        //对敏感信息进行脱敏处理
        systemUser.setPassword(null);

        SystemPost systemPost = systemPostService.getById(systemUser.getPostId());
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        systemUserItemVo.setPostId(systemPost.getId());
        systemUserItemVo.setPostName(systemPost.getName());
        BeanUtils.copyProperties(systemUser, systemUserItemVo);

        return systemUserItemVo;
    }

    @Override
    public void saveOrUpdateSystemUser(SystemUser systemUser) {
        //如果密码有值的话，那么我们就把密码加密处理
        if (StringUtils.hasText(systemUser.getPassword())) {
            //加密处理
            //加盐加密算法
            String password = DigestUtils.sha256Hex(systemUser.getPassword() + systemUser.getUsername());
            systemUser.setPassword(password);
        }
        this.saveOrUpdate(systemUser);

    }

    @Override
    public Boolean isUserNameAvailable(String username) {
        //根据username查询，如果值为0，则代表没有，那么可用，反之不可用
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername, username);
        long count = this.count(queryWrapper);
        return count == 0;

    }

    @Override
    public void updateStatusByUserId(Long id, BaseStatus status) {
        LambdaUpdateWrapper<SystemUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemUser::getId, id)
                .set(SystemUser::getStatus, status);
        this.update(updateWrapper);

    }
}




